/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dsrv6.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Dsrv6.persistence.entities.Register;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import Dsrv6.persistence.controllers.exceptions.NonexistentEntityException;
import Dsrv6.persistence.entities.Gender;

/**
 *
 * @author a381u296
 */
public class GenderJpaController implements Serializable {

    public GenderJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Gender gender) {
        if (gender.getRegisterList() == null) {
            gender.setRegisterList(new ArrayList<Register>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Register> attachedRegisterList = new ArrayList<Register>();
            for (Register registerListRegisterToAttach : gender.getRegisterList()) {
                registerListRegisterToAttach = em.getReference(registerListRegisterToAttach.getClass(), registerListRegisterToAttach.getRegId());
                attachedRegisterList.add(registerListRegisterToAttach);
            }
            gender.setRegisterList(attachedRegisterList);
            em.persist(gender);
            for (Register registerListRegister : gender.getRegisterList()) {
                Gender oldGndGndIdOfRegisterListRegister = registerListRegister.getGndGndId();
                registerListRegister.setGndGndId(gender);
                registerListRegister = em.merge(registerListRegister);
                if (oldGndGndIdOfRegisterListRegister != null) {
                    oldGndGndIdOfRegisterListRegister.getRegisterList().remove(registerListRegister);
                    oldGndGndIdOfRegisterListRegister = em.merge(oldGndGndIdOfRegisterListRegister);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Gender gender) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Gender persistentGender = em.find(Gender.class, gender.getGndId());
            List<Register> registerListOld = persistentGender.getRegisterList();
            List<Register> registerListNew = gender.getRegisterList();
            List<Register> attachedRegisterListNew = new ArrayList<Register>();
            for (Register registerListNewRegisterToAttach : registerListNew) {
                registerListNewRegisterToAttach = em.getReference(registerListNewRegisterToAttach.getClass(), registerListNewRegisterToAttach.getRegId());
                attachedRegisterListNew.add(registerListNewRegisterToAttach);
            }
            registerListNew = attachedRegisterListNew;
            gender.setRegisterList(registerListNew);
            gender = em.merge(gender);
            for (Register registerListOldRegister : registerListOld) {
                if (!registerListNew.contains(registerListOldRegister)) {
                    registerListOldRegister.setGndGndId(null);
                    registerListOldRegister = em.merge(registerListOldRegister);
                }
            }
            for (Register registerListNewRegister : registerListNew) {
                if (!registerListOld.contains(registerListNewRegister)) {
                    Gender oldGndGndIdOfRegisterListNewRegister = registerListNewRegister.getGndGndId();
                    registerListNewRegister.setGndGndId(gender);
                    registerListNewRegister = em.merge(registerListNewRegister);
                    if (oldGndGndIdOfRegisterListNewRegister != null && !oldGndGndIdOfRegisterListNewRegister.equals(gender)) {
                        oldGndGndIdOfRegisterListNewRegister.getRegisterList().remove(registerListNewRegister);
                        oldGndGndIdOfRegisterListNewRegister = em.merge(oldGndGndIdOfRegisterListNewRegister);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = gender.getGndId();
                if (findGender(id) == null) {
                    throw new NonexistentEntityException("The gender with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Gender gender;
            try {
                gender = em.getReference(Gender.class, id);
                gender.getGndId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The gender with id " + id + " no longer exists.", enfe);
            }
            List<Register> registerList = gender.getRegisterList();
            for (Register registerListRegister : registerList) {
                registerListRegister.setGndGndId(null);
                registerListRegister = em.merge(registerListRegister);
            }
            em.remove(gender);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Gender> findGenderEntities() {
        return findGenderEntities(true, -1, -1);
    }

    public List<Gender> findGenderEntities(int maxResults, int firstResult) {
        return findGenderEntities(false, maxResults, firstResult);
    }

    private List<Gender> findGenderEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Gender.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Gender findGender(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Gender.class, id);
        } finally {
            em.close();
        }
    }

    public int getGenderCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Gender> rt = cq.from(Gender.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
